package com.winit.openapi.apiadapter.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.mms.spi.command.ItemForCustomerCommand;
import com.winit.mms.spi.command.ModifyProductForCustomerCommand;
import com.winit.mms.spi.vo.ProductCheckErrorMessagesVO;
import com.winit.openapi.apiadapter.adapter.MMS3ItemServiceAdapter;
import com.winit.openapi.apiadapter.controller.builder.MerchandiseBuilder;
import com.winit.openapi.apiadapter.controller.validator.MerchandiseValidator;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

@Controller
public class MerchandiseAdapteController extends BaseController {
    
    private static final Logger                    logger = LoggerFactory.getLogger(MerchandiseAdapteController.class);
    
    @Resource
    private MerchandiseBuilder     merchandiseBuilder;
    @Resource
    private MMS3ItemServiceAdapter mms3ItemServiceAdapter;

    @RequestMapping(value = "/registerProduct")
    public String registerMerchandise(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        
        logger.info("---------openapi registerProduct start...--------");

        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        // 1.校验协议
        mms3ItemServiceAdapter.checkCustomerAgreement();
        // 2.校验入参
        MerchandiseValidator.validateRegisterMerchandise(json);

        ItemForCustomerCommand registerCommand = merchandiseBuilder.buildMerchandiseregister(json);

        // 调用导入
        List<ProductCheckErrorMessagesVO> checkErrorMessagesVOs = mms3ItemServiceAdapter.registerItemForCustomer(registerCommand);
        responseMsg.setData(checkErrorMessagesVOs);
        if (checkErrorMessagesVOs.size() > 0) {
            // ErrCode.setErrorMsg(ctx, responseMsg, ErrCode.API_SYSTEM_ERROR);
            // return responseMsg;
            throw new ApiException(ErrorCode.MMS_MERCHANDISE_REGISTER_FAIL);
        }

        responseMsg.setData(merchandiseBuilder.buildRegisterResult(json));

        return API_ADAPTED_SUCCESS;
    }

    @RequestMapping(value = "/updateProductInfomationTest")
    public String updateMerchandiseInfo(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        JSONObject json = JSONObject.parseObject(requestMsg.getData().toString());
        // 1.校验协议
        mms3ItemServiceAdapter.checkCustomerAgreement();
        // 2.校验入参
        MerchandiseValidator.validateUpdateMerchandise(json);

        ModifyProductForCustomerCommand updateCommand = merchandiseBuilder.buildMerchandiseUpdate(json);

        List<ProductCheckErrorMessagesVO> erorrList = mms3ItemServiceAdapter.modifyItemForCustomer(updateCommand);
        responseMsg.setData(erorrList);
        if (erorrList.size() > 0) {
            throw new ApiException(ErrorCode.MMS_MERCHANDISE_UPDATE_FAIL);
        }

        return API_ADAPTED_SUCCESS;
    }
    

}
